package com.gkj.blog.business.service.impl;

import com.gkj.blog.business.service.UserRoleService;
import com.gkj.blog.business.vo.entity.UserRole;
import com.gkj.blog.framework.object.AbstractServiceImpl;
import com.gkj.blog.persistence.mapper.SysUserMapper;
import com.gkj.blog.persistence.mapper.SysUserRoleMapper;
import com.gkj.blog.persistence.po.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserRoleServiceImpl extends AbstractServiceImpl<UserRole,SysUserRole> implements UserRoleService {
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    public void setSysUserRoleMapper(SysUserRoleMapper sysUserRoleMapper) {
        this.sysUserRoleMapper = sysUserRoleMapper;
        setBaseMapper(sysUserRoleMapper);
    }


    @Override
    public boolean removeByUserId(Long id) {
        return sysUserRoleMapper.removeByUserId(id)>=0;
    }

    @Override
    public boolean setRoles(Long userId, Long[] roleIds) {
        if(roleIds==null||roleIds.length==0){
            return false;
        }
        removeByUserId(userId);
        for(Long roleId:roleIds){
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            insert(userRole);
        }
        return true;
    }
}
